home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_01 / ed_157 / find_string.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-11  |  3.1 KB  |  117 lines

  1. /*
  2.  * Copyright (C) 1992 by Rush Record (rhr@clio.rice.edu)
  3.  * 
  4.  * This file is part of ED.
  5.  * 
  6.  * ED is free software; you can redistribute it and/or modify it under the terms
  7.  * of the GNU General Public License as published by the Free Software Foundation.
  8.  * 
  9.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License along with ED
  14.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  15.  * Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17. #include "opsys.h"
  18.  
  19. #include "rec.h"
  20. #include "window.h"
  21. #include "ed_dec.h"
  22. #include "buffer.h"
  23.  
  24. static rec_ptr saverec;    /* these are so ALL can quickly continue search from found one */
  25. static Int savebyt;
  26.  
  27. /******************************************************************************\
  28. |Routine: find_string
  29. |Callby: do_grep edit
  30. |Purpose: Implements string search. Returns the offset of the string from
  31. |         the current position, or zero if not enough matches were found.
  32. |Arguments:
  33. |    rec is the record from which the search starts.
  34. |    byt is the byte offset in that record.
  35. |    dir is the direction of the search (-1 means backwards, 1 forwards).
  36. |    search is the buffer containing the search string.
  37. |    flags is the array of search flags. See match_search for a description.
  38. |    repeat is the repeat count applied to the search.
  39. \******************************************************************************/
  40. Int find_string(rec,byt,dir,search,flags,repeat)
  41. register rec_ptr rec;
  42. register Int byt,dir;
  43. buf_ptr search;
  44. Char *flags;
  45. Int repeat;
  46. {
  47.     register Int i;
  48.     Int offset;
  49.  
  50.     match_init(flags);
  51.     offset = 0;
  52.     for(i = 0;i < repeat;i++)
  53.     {
  54.         if(dir > 0)
  55.         {
  56.             while(rec != BASE)
  57.             {
  58.                 if(++byt > rec->length)
  59.                 {
  60.                     byt = 0;
  61.                     if((rec = rec->next) == BASE)
  62.                         return(0);
  63.                 }
  64.                 offset++;
  65.                 if(match_search(rec,byt,search))
  66.                     goto next;
  67.             }
  68.             return(0);
  69.         }
  70.         else    /* search backwards */
  71.         {
  72.             if(rec == BASE && rec->prev != BASE)
  73.             {
  74.                 rec = rec->prev;
  75.                 byt = rec->length;
  76.                 offset--;
  77.             }
  78.             while(rec != BASE)
  79.             {
  80.                 if(--byt < 0)
  81.                 {
  82.                     if((rec = rec->prev) == BASE)
  83.                         return(0);
  84.                     byt = rec->length;
  85.                 }
  86.                 offset--;
  87.                 if(match_search(rec,byt,search))
  88.                     goto next;
  89.             }
  90.             return(0);
  91.         }
  92. next : 
  93.         ;
  94.     }
  95.     saverec = rec;
  96.     savebyt = byt;
  97.     return(offset);
  98. }
  99.  
  100. /******************************************************************************\
  101. |Routine: get_string_loc
  102. |Callby: do_grep
  103. |Purpose: Allows caller to get string match position, as starting point for further searches.
  104. |         NOTE: this routine returns garbage if the last call to find_string didn't return a hit.
  105. |Arguments:
  106. |    rec is the returned record pointer.
  107. |    byt is the returned byte offset within the record.
  108. \******************************************************************************/
  109. void get_string_loc(rec,byt)
  110. rec_ptr *rec;
  111. Int *byt;
  112. {
  113.     *rec = saverec;
  114.     *byt = savebyt;
  115. }
  116.  
  117.